﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using TiTi.tea.Comm;
using TiTi.tea.DAL;
using TiTi.tea.Dto;
using TiTi.tea.Model;

namespace TiTi.tea.BLL
{
    public class CategoryBLL
    {
        private CategoryDAL cateDal = new CategoryDAL();

        /// <summary>
        /// 获取全部分类
        /// </summary>
        /// <returns></returns>
        public IEnumerable<CategoryDto> GetAll()
        {
            return cateDal.GetAll(o => o.IsDelete == 0).Select(o => new CategoryDto()
            {
                Id = o.Id,
                CatName = o.CatName,
                Icon = o.Icon
            });
        }

        /// <summary>
        /// 获取分类以及分类下的产品
        /// </summary>
        /// <returns></returns>
        public IEnumerable<CategoryProductDto> GetCategoryProducts()
        {
            var spuDal = new SpuDAL();

            var cates = cateDal.GetAll(o => o.IsDelete == 0 && o.IsShow == 1);
            var spus = spuDal.GetAll(o => o.IsDelete == 0);

            return (from cate in cates
                    select new CategoryProductDto()
                    {
                        id = cate.Id,
                        name = cate.CatName,
                        sort = cate.Sort ?? 0,
                        image = null,
                        products = spus.Where(o => o.CategoryId == cate.Id).ToList().Select(s=>new SpuProductDto()
                        {
                            id=s.Id,
                            name=s.SpuName,
                            description=s.Introduction,
                            is_single=s.IsEnableMaterial==0,
                            is_enableMaterial = s.IsEnableMaterial==1,
                            support_takeaway=1,
                            ImageUrls=s.ImageUrls,
                            image = s.ImageUrl,
                            SpecItems = s.SpecItems,
                            price = s.Price??0,
                            MaterialItems = s.MaterialItems
                            
                        })
                    }).Where(o => o.products.Count() > 0).ToList();
        }
    }
}
